#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void save(void)
{
	
	char path[PATH_MAX];
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("存款：acc path:%s\n",path);
	int fd = open(path,O_RDWR);
	Ctos save;
	read(fd,&save.acc,sizeof(save.acc));
	int i= save.acc.money;	
	save.acc.money = i+ctos.acc.money;
	lseek(fd,0,SEEK_SET);
	write(fd,&save.acc,sizeof(save.acc));
	sprintf(stoc.str,"恭喜您存款成功，存款金额为:%f,帐号余额为：%f  ",ctos.acc.money,save.acc.money);	
	close(fd);	
	// 返回存款成功信息
	stoc_send(&stoc);
}


int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_SAVE);
		stoc.type = ctos.pid;
		save();
	}
}
